﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Extensions.Configuration;

using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;

namespace SHZY.Business.Entitys.Common
{
    public static class HttpCommon
    {
        #region 服务地址
        public static string HostUrl = App.Configuration.GetValue<string>("SHZY_App:SyncYiFeiDataUrl");
        #endregion

        #region HttpPost提交
        public static async Task<string> HttpPost(string apiUrl, IDictionary<string, string> queryStrings)
        {
            try
            {
                var queryString = string.Join("&", queryStrings.Select(s => $"{s.Key}={s.Value}"));
                var url = $"{HostUrl}/{apiUrl}?{queryString}";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                Stream requestStream = request.GetRequestStream();
                StreamWriter myStreamWriter = new StreamWriter(requestStream);

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string encoding = response.ContentEncoding;
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
                    string retString = await myStreamReader.ReadToEndAsync();
                    myStreamReader.Close();
                    return retString;
                }
                return string.Empty;
            }
            catch (Exception e)
            {
                Console.WriteLine("有异常了!");
                return "";
            }
        }
        #endregion
    }
}
